package com.hlkj.baojin.utils.hik;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;


public class RestUtil {
	 public static String load(String url,String query) throws Exception
	    {

	    URL restURL = new URL(url);

	        HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();

	        conn.setRequestMethod("POST");

	        conn.setDoOutput(true);

	        conn.setAllowUserInteraction(false);

	        PrintStream ps = new PrintStream(conn.getOutputStream());
	        ps.print(query);

	        ps.close();

	        BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

	        String line,resultStr="";

	        while(null != (line=bReader.readLine()))

	        {

	        resultStr +=line;

	        }

	        bReader.close();

	        return resultStr;

	    }
	 
	  public static String sendGet(String url, String param) {
	        String result = "";
	        BufferedReader in = null;
	        try {
	            String urlNameString = url + "?" + param;
	            //String str = new String(urlNameString.getBytes("iso8859-1"), "utf-8");
	            //System.out.println(str);
	            URL realUrl = new URL(urlNameString);
	            System.out.println(realUrl);
	            URLConnection connection = realUrl.openConnection();
	            
	            connection.setRequestProperty("accept", "*/*");
	            connection.setRequestProperty("connection", "Keep-Alive");
	            connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; utf-8; rv:1.9.2.6)");
	            connection.setRequestProperty("Charset", "utf-8");
	            
	            connection.connect();
	            //Map<String, List<String>> map = connection.getHeaderFields();
	            
	            in = new BufferedReader(new InputStreamReader(
	                    connection.getInputStream()));
	            String line;
	            while ((line = in.readLine()) != null) {
	                result += line;
	            }
	        } catch (Exception e) {
	            System.out.println("发送GET请求出现异常！" + e);
	            e.printStackTrace();
	        }
	        finally {
	            try {
	                if (in != null) {
	                    in.close();
	                }
	            } catch (Exception e2) {
	                e2.printStackTrace();
	            }
	        }
	        return result;
	    }
}
